<?php

class DBMySQLi extends mysqli
{
    public function __construct( $host, $user, $passwd, $dbname )
    {
        try {
            @parent::__construct($host,$user,$passwd,$dbname);
        } catch (Exception $e) {
        if (mysqli_connect_error()) {
            throw new DBException('Connection failed ['.mysqli_connect_errno().']:'.mysqli_connect_error());
        }}
    }
    
    /**
     * Prepares the SQL query pointed to by the null-terminated string query, 
     * and returns a statement handle to be used for further operations on the statement. 
     * The query must consist of a single SQL statement.
     *
     * @param string $sql
     * @return StmtMySQLi
     */
    public function prepare($sql, $types = NULL)
    {
        return new StmtMySQLi($this,$sql,$types);
    }
    /**
     * Performs a query against the Database
     *
     * @param string $sql
     * @return ResultMySQLi
     */
    public function query($sql)
    {
        if (!$this->real_query($sql)) {
            throw new DBException($this->error, $this->errno);
        }
        if (!empty($this->error)) {
            throw new SQLException($this->error,$this->errno);
        }
        return new ResultMySQLi($this);
    }
        
    /**
     * Used to initiate the retrieval of a result set from the last query executed using the real_query() function on the database connection. 
     *
     * @return ResultMySQLi
     */
    public function use_result()
    {
        return new ResultMySQli($this);
    }
    /**
     * 
     *
     * @return ResultMySQLi
     */
    public function store_result()
    {
        return new ResultMySQli($this);
    }
}

